﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using JEdwonTune.Lib;
using System.Diagnostics;
using Chilkat;
using System.IO;
using System.Security.AccessControl;

namespace JskitPublish {
    public partial class FrmJskitPublish : Form {
        public FrmJskitPublish() {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e) {
            this.Close();
        }

        private Boolean _Installing = false;
        public Boolean Installing {
            set { _Installing = value; }
            get { return _Installing; }
        }

        private void btnPublish_Click(object sender, EventArgs e) {
            //检查是否有jskit.org发布
            Boolean dirExists = IISHelper.CheckVirtualDirExists(MyUtil.JskitVirtualDir);
            if (dirExists && !this.cbOverride.Checked) return;
            //解压jskit.org
            Boolean unziped = UnzipJskitOrg();
            //发布jskit.org
            if (unziped) {
                try {
                    String realPath = Directory.GetCurrentDirectory()+"\\jskit.org";
                    IISHelper.PublishVirtualDir(MyUtil.JskitVirtualDir, realPath, this.cbOverride.Checked);
                    System.Diagnostics.Process.Start("iexplore", "http://localhost/jskit.org/index.html");
                } catch (Exception ex) {
                    MessageBox.Show(ex.Message, "发布时错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            this.Close();
        }

        private Boolean UnzipJskitOrg() {
            Rar rar = new Rar();
            Boolean success; 
            success = rar.Open("jskit.org.rar");//指定要解压的rar文件 
            if (success != true) {
                MessageBox.Show(rar.LastErrorText, "解压文件时错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            try {
                if (Directory.Exists("jskit.org")) {
                    Directory.Delete("jskit.org", true);
                }
            } catch (Exception ex) {
                MessageBox.Show(ex.Message, "删除旧文件时错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            Directory.CreateDirectory("jskit.org");
            success = rar.Unrar("jskit.org");//指定要解压的路径，该路径要存在，如果没有的话要先创建 
            if (success != true) {
                MessageBox.Show(rar.LastErrorText, "解压文件时错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            return true;
        }

        private void FrmJskitPublish_Load(object sender, EventArgs e) {
            if (_Installing) {
                this.btnCancel.Text = "跳过";
            } else {
                this.btnCancel.Text = "取消";
            }
        }
    }
}
